#define  _CRT_SECURE_NO_WARNINGS
class Solution {
    int tgt;
    vector<vector<int>> ret;
    vector<int> path;
    // void dfs(vector<int>& candidates, int left, int index = 0)
    // {
    //     if (left <= 0)
    //     {
    //         if (!left)
    //         {
    //             ret.push_back(path);
    //         }
    //         return;
    //     }
    //     for (int i = index; i < candidates.size(); i++)
    //     {
    //         path.push_back(candidates[i]);
    //         dfs(candidates, left - candidates[i], i);
    //         path.pop_back();
    //     }
    // }
    void dfs(vector<int>& nums, int left, int index = 0)
    {
        if (index == nums.size())
        {
            if (left == 0)
            {
                ret.push_back(path);
            }
            return;
        }
        int cur = nums[index];
        int i = 0;
        for (; left >= 0; i++)
        {
            dfs(nums, left, index + 1);
            path.push_back(cur);
            left -= cur;
        }
        while (i--) {
            path.pop_back();
        }
    }

public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        tgt = target;
        dfs(candidates, target);
        return ret;
    }
};